package com.ebook.ui.fragment.home.shuffle;

import com.ebook.ui.activity.HomeActivity;
import com.ebook.ui.sync.WorkSyncer;
import com.ebook.ui.workspace.EBookWebView;

import javax.swing.*;
import java.util.ArrayList;
/**
 * 该类为ShufflingFragment富华前的合成类,但是合成的逻辑不是如此,故该类不再使用.
 * **/
@Deprecated
public class ShufflingComponent extends JPanel {
    private ArrayList<ShufflingItem> shuffleItems;
    private volatile boolean go = true;
    private int point = 0;
    private JLabel view;
    private Thread shuffleThread;
    private Thread sleepThread;
    private volatile int clock;
    public ShufflingComponent(ArrayList<ShufflingItem> shuffleItems){
        super();
        this.shuffleItems = shuffleItems;
        view = new JLabel(new ImageIcon
                (shuffleItems.get(point).getImg_path())
        );
        this.add(view);
        view.setBounds(0,0,696,390);
        this.setBounds(0,0,696,390);
        shuffleThread = getShuffleThread();
        shuffleThread.start();
        Thread t1 = new Thread(()->{
            while(true){
                try{
                    Thread.sleep(100);
                    clockA();
//                    System.out.println(getClock()+" go"+go);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
    }
    public synchronized void clockA(){
        clock = clock + 1;
    }
    public synchronized int getClock(){
        return clock;
    }
    public void turnToPre(){
        point = point - 1;
        if(point<0){
            point = this.shuffleItems.size()-1;
        }
        view.setIcon(new ImageIcon(shuffleItems.get(point).getImg_path()));
    }
    public void turnToNext(){
        point = point + 1;
        if(point>=this.shuffleItems.size()){
            point = 0;
        }
        view.setIcon(new ImageIcon(shuffleItems.get(point).getImg_path()));
    }
    public void sleep(){
        go = false;
        if(sleepThread!=null){
            sleepThread.interrupt();
        }
        sleepThread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    go = true;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        sleepThread.start();
    }
    public Thread getShuffleThread(){
        return new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    while(go){
                        try{
//                            System.out.println(getClock()+"go=="+go);
                            if(WorkSyncer.webView==null){
                                Thread.sleep(2000);
                            }else if(WorkSyncer.webView.peekActivity() instanceof HomeActivity){
                                SwingUtilities.invokeLater(new Runnable(){
                                    @Override
                                    public void run() {
                                        ShufflingComponent.this.turnToNext();
                                    }
                                });
                                Thread.sleep(2000);
//                            System.out.println("在EDT中运行"+SwingUtilities.isEventDispatchThread());
                            }

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }
}
